package aar.test.tessocr.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import aar.test.tessocr.adapter.interfaces.BaseAdapter;
import aar.test.tessocr.model.LanListModel;
import aar.test.tessocr.utils.Utils;
import clx.tessocr.ocreng.R;

/**
 * Created by sunshine on 2020/10/20
 */
public class LanListAdapter extends BaseAdapter< LanListAdapter.OrderListItemHolder> {
    private List<LanListModel> mList;
    private Context context;
    private boolean isUnTaken;
    private boolean isPayed;


    public LanListAdapter(Context mContext, @NonNull List<LanListModel> mList, boolean isUnTaken, boolean isPayed) {
        this.mList = mList;
        this.context = mContext;
        this.isUnTaken = isUnTaken;
        this.isPayed = isPayed;
    }

    @Override
    public LanListAdapter.OrderListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.lan_list_item, parent, false);
        LanListAdapter.OrderListItemHolder holder = new LanListAdapter.OrderListItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull LanListAdapter.OrderListItemHolder holder, final int position) {
        holder.order_id.setText(mList.get(position).getOrderId());


        holder.order_phone.setText(mList.get(position).getPhone());
        if (isUnTaken) {
            holder.ll_button.setVisibility(View.GONE);

            /*holder.tv_return_id.setVisibility(View.GONE);
            holder.tv_already_cash.setVisibility(View.GONE);
            holder.tv_hurry_up.setVisibility(View.GONE);
            holder.order_play_number_phone.setVisibility(View.GONE);*/
        } else {
            holder.ll_button.setVisibility(View.VISIBLE);
            /*holder.tv_return_id.setVisibility(View.VISIBLE);
            holder.tv_already_cash.setVisibility(View.VISIBLE);
            holder.tv_hurry_up.setVisibility(View.VISIBLE);
            holder.order_play_number_phone.setVisibility(View.VISIBLE);*/
            if (!mList.get(position).getExpress_order().equals("") && mList.get(position).getExp_type().equals("0")) {
                holder.tv_clip.setVisibility(View.VISIBLE);
                holder.tv_clip.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utils.setClipboard(context, mList.get(position).getExpress_order());
                        Toast.makeText(context, "复制成功！", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                holder.tv_clip.setVisibility(View.GONE);
            }

            if (isPayed) {
                holder.tv_already_cash.setVisibility(View.GONE);
                holder.tv_hurry_up.setVisibility(View.GONE);
            } else {
                holder.tv_already_cash.setVisibility(View.VISIBLE);
                holder.tv_hurry_up.setVisibility(View.VISIBLE);
            }
        }

        holder.express_id.setText(mList.get(position).getExpress_order());

        if (!TextUtils.isEmpty(mList.get(position).getExpress_order())) {
            holder.cost.setVisibility(View.VISIBLE);
            holder.cost.setText("运费" + mList.get(position).getFreight() + "元");
            holder.tv_return_id.setVisibility(View.GONE);
        } else {
            holder.cost.setVisibility(View.GONE);
            holder.tv_return_id.setVisibility(View.VISIBLE);
        }

        //holder.grid_type.setText(mList.get(position).getGridType());
        //holder.grid_address.setText(mList.get(position).getGridNum() + "格");

        holder.cabinet_address.setText(mList.get(position).getCabinetAddress());

        if (mList.get(position).getExp_type().equals("0")) {
            holder.cabinet_statues.setText("微信下单");
            holder.rl_post_address.setVisibility(View.VISIBLE);
            holder.rl_get_address.setVisibility(View.VISIBLE);
            holder.tv_receiver_clip.setVisibility(View.VISIBLE);
            holder.tv_receiver_clip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Utils.setClipboard(context, mList.get(position).getReceiverDetail());
                    Toast.makeText(context, "复制成功！", Toast.LENGTH_SHORT).show();
                }
            });
            holder.tv_sender_clip.setVisibility(View.VISIBLE);
            holder.tv_sender_clip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Utils.setClipboard(context, mList.get(position).getSenderDetail());
                    Toast.makeText(context, "复制成功！", Toast.LENGTH_SHORT).show();
                }
            });
            holder.post_address.setText(mList.get(position).getSenderDetail());
            holder.get_address.setText(mList.get(position).getReceiverDetail());
            holder.tv_post_id.setText("寄件码");
            holder.txt_post_id.setText(mList.get(position).getExp_code());
        } else {
            holder.cabinet_statues.setText("菜鸟裹裹");
            holder.rl_post_address.setVisibility(View.GONE);
            holder.rl_get_address.setVisibility(View.GONE);
            holder.tv_receiver_clip.setVisibility(View.GONE);
            holder.tv_sender_clip.setVisibility(View.GONE);
            holder.tv_post_id.setText("揽件码");
            holder.txt_post_id.setText(mList.get(position).getPackageId());
        }

        //取件码
        holder.txt_pickup_id.setText(mList.get(position).getCode());

        //物流
        holder.txt_express.setText(mList.get(position).getCabinetExpressName());

        holder.cabinet_time.setText(mList.get(position).getCreated_at());


        if (!mList.get(position).getExp_type().equals("0")) {
            holder.tv_already_cash.setVisibility(View.GONE);
            holder.tv_hurry_up.setVisibility(View.GONE);
            holder.tv_return_id.setVisibility(View.GONE);
            holder.rl_express_cost.setVisibility(View.GONE);
            //holder.tv_express.setVisibility(View.GONE);
            //holder.txt_express.setVisibility(View.GONE);

        } else {
            holder.rl_express_cost.setVisibility(View.VISIBLE);
            //holder.tv_express.setVisibility(View.VISIBLE);
            //holder.txt_express.setVisibility(View.VISIBLE);
        }

        //回传单号
        holder.tv_return_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mViewClickListener) {
                    mViewClickListener.returnId(position);
                }
            }
        });

        //已订单付
        holder.tv_already_cash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mViewClickListener) {
                    mViewClickListener.post_again(position);
                }
            }
        });

        //催款
        holder.tv_hurry_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mViewClickListener) {
                    mViewClickListener.gridTackBack(position);
                }
            }
        });

        holder.order_play_number_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(mList.get(position).getPhone())) {


                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + mList.get(position).getPhone()));
                    //开启系统拨号器
                    context.startActivity(intent);


                } else {
                    Toast.makeText(context, "电话号码为空", Toast.LENGTH_SHORT).show();


                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class OrderListItemHolder extends RecyclerView.ViewHolder {
        TextView order_id;
        TextView order_phone;
        TextView tv_return_id;
        TextView express_id;
        TextView grid_address;
        TextView cabinet_address;
        TextView cabinet_statues;
        TextView cabinet_time;
        LinearLayout ll_button;
        RelativeLayout rl_post_address;
        RelativeLayout rl_get_address;
        TextView tv_hurry_up;
        TextView tv_already_cash;
        TextView order_play_number_phone;
        TextView post_address;
        TextView get_address;
        TextView tv_post_id;
        TextView txt_post_id;
        TextView txt_pickup_id;
        TextView cost;
        TextView txt_express;
        TextView tv_express;
        TextView tv_clip;
        TextView tv_receiver_clip;
        TextView tv_sender_clip;
        RelativeLayout rl_express_cost;

        public OrderListItemHolder(View itemView) {
            super(itemView);
            order_id = itemView.findViewById(R.id.txt_order_num);
            order_phone = itemView.findViewById(R.id.order_phone);
            tv_return_id = itemView.findViewById(R.id.tv_return_id);
            express_id = itemView.findViewById(R.id.express_id);
            grid_address = itemView.findViewById(R.id.grid_address);
            cabinet_address = itemView.findViewById(R.id.cabinet_address);
            cabinet_statues = itemView.findViewById(R.id.cabinet_statues);
            cabinet_time = itemView.findViewById(R.id.cabinet_time);
            ll_button = itemView.findViewById(R.id.ll_button);
            tv_hurry_up = itemView.findViewById(R.id.tv_hurry_up);
            tv_already_cash = itemView.findViewById(R.id.tv_already_cash);
            order_play_number_phone = itemView.findViewById(R.id.order_play_number_phone);
            post_address = itemView.findViewById(R.id.post_address);
            get_address = itemView.findViewById(R.id.get_address);
            rl_get_address = itemView.findViewById(R.id.rl_get_address);
            rl_post_address = itemView.findViewById(R.id.rl_post_address);
            tv_post_id = itemView.findViewById(R.id.tv_post_id);
            txt_post_id = itemView.findViewById(R.id.txt_post_id);
            txt_pickup_id = itemView.findViewById(R.id.txt_pickup_id);
            cost = itemView.findViewById(R.id.cost);
            txt_express = itemView.findViewById(R.id.txt_express);
            tv_express = itemView.findViewById(R.id.tv_express);
            tv_clip = itemView.findViewById(R.id.tv_clip);
            tv_receiver_clip = itemView.findViewById(R.id.tv_receiver_clip);
            tv_sender_clip = itemView.findViewById(R.id.tv_sender_clip);
            rl_express_cost = itemView.findViewById(R.id.rl_express_cost);
        }
    }

    public interface ViewClickListener {
        void post_again(int pos);

        void gridTackBack(int pos);

        void returnId(int pos);
    }

    public void setViewClickListener(LanListAdapter.ViewClickListener mViewClickListener) {
        this.mViewClickListener = mViewClickListener;

    }


    private LanListAdapter.ViewClickListener mViewClickListener;
}

